package priv.conceit.study.book.multithread.chapter8.threadpool;

import java.util.LinkedList;

/**
 * study 自定义任务队列
 *
 * @author Conceit
 * 11/3/2022 增加方法
 * @since 1.0.0, 11/3/2022
 */
public class LinkedRunnableQueue implements RunnableQueue{

	/**
	 * 存放 任务队列 的上限
	 */
	private final int limit;

	/**
	 * 任务队列 满了之后的拒绝策略
	 */
	private final DenyPolicy denyPolicy;

	private final LinkedList<Runnable> runnables=new LinkedList<>();

	/**
	 * 线程池
	 */
	private final ThreadPool threadPool;

	public LinkedRunnableQueue(int limit, DenyPolicy denyPolicy, ThreadPool threadPool) {
		this.limit = limit;
		this.denyPolicy = denyPolicy;
		this.threadPool = threadPool;
	}


	/**
	 * 提交任务 至线程池 任务队列
	 *
	 * @param runnable
	 */
	@Override
	public void push(Runnable runnable) {
		synchronized (runnables){
			if(runnables.size()>=limit){
				denyPolicy.reject(runnable,threadPool);
			}else{
				runnables.addLast(runnable);
				runnables.notifyAll();
			}
		}

	}

	/**
	 * 从线程池 任务队列 获取任务
	 */
	@Override
	public Runnable pull() {
		return null;
	}

	@Override
	public int size() {
		return 0;
	}
}
